//
//  JPSettingAlterSecretVC.m
//  JPKY
//
//  Created by 吴俊杰 on 2016/11/15.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPSettingAlterSecretVC.h"
#import <SVProgressHUD.h>
#import <SSKeychain.h>

@interface JPSettingAlterSecretVC ()

// 原密码输入框
@property (weak, nonatomic) UITextField *oldSecretTextField;
// 新密码输入框
@property (weak, nonatomic) UITextField *nowSecretTextField;
// 新密码确认框
@property (weak, nonatomic) UITextField *nowSecretAgainTextField;

@end

@implementation JPSettingAlterSecretVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self setupSubviews];
}

/**
 初始化子视图
 */
- (void)setupSubviews {
    
    // 原密码Label
    UITextField *oldSecretTextField = [[UITextField alloc] init];
    [self.view addSubview:oldSecretTextField];
    self.oldSecretTextField = oldSecretTextField;
    oldSecretTextField.placeholder = @"请输入原密码";
    // 隐藏密码
    oldSecretTextField.secureTextEntry = YES;
    // 设置边框模式
    oldSecretTextField.borderStyle = UITextBorderStyleRoundedRect;
    // 自动布局
    CGFloat offset = self.view.bounds.size.width * 0.2;
    [oldSecretTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(120);
        make.left.equalTo(self.view).offset(offset);
        make.right.equalTo(self.view).offset(-offset);
    }];
    
    // 新密码Label
    UITextField *nowSecretTextField = [[UITextField alloc] init];
    [self.view addSubview:nowSecretTextField];
    self.nowSecretTextField = nowSecretTextField;
    nowSecretTextField.placeholder = @"请输入新密码";
    nowSecretTextField.secureTextEntry = YES;
    //设置边框模式
    nowSecretTextField.borderStyle = UITextBorderStyleRoundedRect;
    // 自动布局
    [nowSecretTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(oldSecretTextField);
        make.top.mas_equalTo(oldSecretTextField.mas_bottom).offset(30);
        make.left.right.equalTo(oldSecretTextField);
    }];
    
    // 再次输入新密码
    UITextField *nowSecretAgainTextField = [[UITextField alloc] init];
    [self.view addSubview:nowSecretAgainTextField];
    self.nowSecretAgainTextField = nowSecretAgainTextField;
    nowSecretAgainTextField.placeholder = @"请再次输入新密码";
    nowSecretAgainTextField.secureTextEntry = YES;
    // 设置边框模式
    nowSecretAgainTextField.borderStyle = UITextBorderStyleRoundedRect;
    // 自动布局
    [nowSecretAgainTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(nowSecretTextField);
        make.top.mas_equalTo(nowSecretTextField.mas_bottom).offset(30);
        make.left.right.equalTo(nowSecretTextField);
    }];
    
    //确定按钮
    UIButton *sureBtn = [[UIButton alloc] init];
    [self.view addSubview:sureBtn];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"buttonBackground"] forState:UIControlStateNormal];
    // 自动布局
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nowSecretAgainTextField.mas_bottom).offset(50);
        make.left.right.equalTo(nowSecretAgainTextField);
        make.centerX.equalTo(nowSecretAgainTextField);
    }];
}

/**
 确定按钮点击事件
 */
- (void)sureBtnAction:(UIButton *)sender {
    
    // 设置消失时间
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    // 获取电话号码
    NSString *phone = [[NSUserDefaults standardUserDefaults] objectForKey:@"phone"];
    // 获取旧密码
    NSString *secret = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:phone];
    // 判断原密码是否正确
    if(![_oldSecretTextField.text isEqualToString:secret]) {
        [SVProgressHUD showErrorWithStatus:@"旧密码不正确!"];
        return;
    }
    // 判断是否输入新密码
    if (_nowSecretTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"新密码不能为空!"];
        return;
    }
    // 判断两次密码输入是否相同
    if (![_nowSecretTextField.text isEqualToString: _nowSecretAgainTextField.text]) {
        [SVProgressHUD showErrorWithStatus:@"两次密码不一致!"];
    } else {
        // 保存到钥匙串
        [SSKeychain setPassword:self.nowSecretTextField.text forService:[NSBundle mainBundle].bundleIdentifier account:phone];
        // 修改成功后置为空
        self.oldSecretTextField.text = nil;
        self.nowSecretTextField.text = nil;
        self.nowSecretAgainTextField.text = nil;
        [SVProgressHUD showSuccessWithStatus:@"修改成功!"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
